package cn.codewen.servlet;

import cn.codewen.pojo.User;
import cn.codewen.service.UserService;
import cn.codewen.service.impl.CosServiceImpl;
import cn.codewen.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/cos")
public class CosServlet extends HttpServlet {


    //0. 创建userService对象
    // private AdminService adminService=new AdminServiceImpl();
//    private UserService userService = new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 设置字符集
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        //2. 接收参数
        String m = request.getParameter("m");
        //3. 判断
   /*     if (m.equals("list")) {
            list(request, response);
        } else if (m.equals("add")) {
            add(request, response);
        } else if (m.equals("edit")) {
            edit(request, response);
        } else if (m.equals("del")) {
            del(request, response);
        }*/

        switch (m) {
            case "list":
                list(request, response);
                break;
            case "add":
                add(request, response);
                break;
            case "edit":
                edit(request, response);
                break;
            case "del":
                del(request, response);
                break;
            case "addDir":
                addDir(request, response);
                break;
        }

    }

    private void addDir(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        if (request.getMethod().equals("POST")) {
            //1. 接收参数
            User user = (User) request.getSession().getAttribute("user");
            String prefix = request.getParameter("prefix");

            if (prefix==null){
                prefix="";
            }

             String userNamePrefix=user.getUsername()+"/"+prefix+"/";
            //需要创建的目录名
            String dirName = request.getParameter("dir")+"/";

            //最终的目录
            String key = userNamePrefix+dirName;
            //2. 处理数据
            //2.1 创建Service对象
            CosServiceImpl cosService = new CosServiceImpl();
            //2.2 调用addDir方法
            Boolean rs = cosService.addDirs(key);
            //3. 跳转
            if (rs){

                response.sendRedirect("/cos?m=list&prefix="+prefix);
            }

        }else {

            request.getRequestDispatcher("/addDir.jsp").forward(request,response);
        }

    }


    private void del(HttpServletRequest request, HttpServletResponse response) {
    }

    private void edit(HttpServletRequest request, HttpServletResponse response) {
    }

    private void add(HttpServletRequest request, HttpServletResponse response) {
    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    //1.接收参数 codewen  + aa/  =====>codewen/aa/
        //取到当前登录的用户
        User user = (User) request.getSession().getAttribute("user");


        String prefix = request.getParameter("prefix");
        if (prefix == null) {
            prefix = "";
        }

        String username= user.getUsername()+"/";
        String userNamePrefix= username+prefix;

        //2. 处理数据

        CosServiceImpl cosService = new CosServiceImpl();


        List listObjects = cosService.listObjects(userNamePrefix);


        //3. 展示或跳转
        //3.1 传递数据
        request.setAttribute("prefix",prefix);
        request.setAttribute("userNamePrefix",userNamePrefix);
        request.setAttribute("username",username);
        request.setAttribute("listObjects",listObjects);
        //3.2 转发请求
        request.getRequestDispatcher("/list.jsp").forward(request,response);




    }


}
